package org.liukai.servlet_ems.util;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @project servlet_ems
 * @create DBUtil.java 
 * 
 * @author liukai
 * @date 2016/1/18 14:28
 * @detail 
 */
public class DBUtil {
    //静态块加载驱动
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("没有找到驱动");
        }
    }

    //得到连接
    public static Connection getConnection() {
        try {
            Connection conn = DriverManager.getConnection("***", "***", "***");
            return conn;
        } catch (SQLException e) {
            throw new RuntimeException("无法连接DB");
        }
    }

    //关闭连接
    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException("关闭连接出错");
            }
        }
    }

    @Test
    public void test() throws SQLException {
/*        List<Emp> list = EmpDao.queryEmpList();
        for (Emp e : list) {
            System.out.println(e);
        }*/
        /*User user = UserDao.queryUserByName("liukai");
        System.out.println(user);*/
    }
}
